$(function(){
	$("#fix-it-button").button({icons: {secondary: "ui-icon-triangle-1-s"} }).click(function(){ 
		$("#fix-it-instructions").show("bounce", {}, "fast", function(){
			setTimeout(function(){
				$("#fix-it-instructions").hide("fade", {}, "fast");
			}, 5000);
		});
	});
});

getSelected = function(){
	var t = '';
if(window.getSelection){
	t = window.getSelection();
}else if(document.getSelection){
	t = document.getSelection();
}else if(document.selection){
	At = document.selection.createRange().text;
}
return t;
}

$(function(){
	$(document).bind('mouseup', function(event){
		var selection = getSelected();
		if(selection != '')
		{
			var range = selection.getRangeAt(0);
			if(range.startContainer == range.endContainer)
			{
				//console.log(range.startOffset);
				//console.log(range.endOffset);
				//console.log(range.startContainer);
				//console.log(event.pageX);
				//console.log(event.pageY);
				$("#fix-it-popout div").hide();
				$("#fix-it-instructions").hide();
				$("#fix-it-popout-choices").show();
				$("#fix-it-popout").show("fade", {}, "fast").position({my: "left bottom", at: "center top", of: event});
				last_range = range
			}
			else
			{
				$("#fix-it-popout div").hide();
				$("#fix-it-instructions").hide();
				$("#fix-it-popout-error").empty().append("Please select from only one paragraph").show();
				$("#fix-it-popout").show("fade", {}, "fast").position({my: "left bottom", at: "center top", of: event});
			}
		}
		else
		{
			//console.log("unclick")
			$("#fix-it-popout").hide();
		}
	});
});


$(function(){
	$("#fix-it-popout-choices form input").click(function(){
		//$.post("http://127.0.0.1:8000/fixit/post/",
		//console.log( 
//{context: last_range.startContainer.textContent, start_offset: last_range.startOffset, end_offset: last_range.endOffset,error_type: $(this).val(), selected_text: last_range.toString(), url: document.URL}, function(resp){console.log(resp);});
		$.get("http://edithuddle.appspot.com/fixit", {content: last_range.startContainer.textContent, start_offset: last_range.startOffset, end_offset: last_range.endOffset,errorType: $(this).val(), errorContent: last_range.toString(), url: document.URL, author:"John Smith",title:"My Blog Post"});

		$("#fix-it-popout div").hide();
		$("#fix-it-popout-thanks").show();
		$("#fix-it-popout input").val([]);
		setTimeout(function(){
			$("#fix-it-popout").hide("fade",{}, "slow");
		}, 4000);
	});
});

